package com.findmeastore.util;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;

import com.findmeastore.bo.ContactDetails;
import com.findmeastore.dto.ContactDto;

public class BeanCopyUtil {
	
	static {
		// required to convert default values of primitives to NULL
		ConvertUtils.deregister();
		ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
		ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
		ConvertUtils.register(new BooleanConverter(null), Boolean.class);
		ConvertUtils.register(new ByteConverter(null), Byte.class);
		ConvertUtils.register(new CharacterConverter(null), Character.class);
		ConvertUtils.register(new DoubleConverter(null), Double.class);
		ConvertUtils.register(new FloatConverter(null), Float.class);
		ConvertUtils.register(new IntegerConverter(null), Integer.class);
		ConvertUtils.register(new LongConverter(null), Long.class);
		ConvertUtils.register(new ShortConverter(null), Short.class);
	}
	
	public static void copy(ContactDetails contactDetails, ContactDto contactDto) {
		try {
			BeanUtils.copyProperties(contactDetails, contactDto);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void copy(ContactDto contactDto, ContactDetails contactDetails) {
		try {
			BeanUtils.copyProperties(contactDto, contactDetails);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
